<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Selfhelp\Controller;
use Think\Controller;

class AddplateController extends BaseController {

    public function addp(){

		/*接受传递过来的参数*/
		$shelfid = I('get.shelfid'); //当前码的ID号
		$plateid = I('get.plateId');
		$nowid = I('get.nowid');
		$nowplateid = I('get.nowplateid');
		/*开始数据库查询比对*/
		/*实例化数据库*/
		$selftoplate = D(C('TABLE_SELFHELPRESOURCEPLATE'));
		/*根据当前码的ID号获取记录条数*/
		$rescount = $selftoplate->where(array('selfhelpresourceid'=>$shelfid))->count();
		if($rescount<2){
			$res = $selftoplate->where(array('selfhelpresourceid'=>$shelfid,'plateid'=>$nowplateid,'id'=>$nowid))->find();
			if($res){
				if($this->saveplatetores($nowid,$plateid,$nowplateid)){
					echo 1;	//SUCCESS
				}else{
					echo 0;	//ERROR
				}
			}else{
				if($this->addplatetores($shelfid,$plateid)){
					echo 1;	//SUCCESS
				}else{
					echo 0;	//ERROR
				}
			}
		}else{
			$res = $selftoplate->fetchSql(true)->where(array('selfhelpresourceid'=>$shelfid,'plateid'=>$nowplateid,'id'=>$nowid))->find();
			if($res){
				if($this->saveplatetores($nowid,$plateid,$nowplateid)){
					echo 1;	//SUCCESS
				}else{
					echo 0;	//ERROR
				}
			}
		}
    }

	/*
	 * 删除添加的板块
	 */
	public function binddelete(){
		$id = I('get.shelfid');
		if(!empty($id)){
			$pres = D(C('TABLE_SELFHELPRESOURCEPLATE'));
			$res = $pres->delete($id);
			if($res){
				echo 1;//SUCCESS
			}else{
				echo 0;//ERROR
			}
		}else{
			echo 2;//参数错误
		}
	}

	/*
	 * 更新操作
	 */
	protected function saveplatetores($nowid,$plateid,$nowplateid=null){
		$selftoplate = D(C('TABLE_SELFHELPRESOURCEPLATE'));
		$data['plateid'] = $plateid;
		$res = $selftoplate->where(array('id'=>$nowid,'plateid'=>$nowplateid))->save($data);
		if($res || $res==0) {
			return true;	//SAVE SUCCESS
		}else{
			return false;	//SAVE ERROR
		}
	}

	/*
	 * 新增操作
	 */
	protected function addplatetores($shelfid,$plateid){
		$selftoplate = D(C('TABLE_SELFHELPRESOURCEPLATE'));
		$selftoplate->selfhelpresourceid = $shelfid;
		$selftoplate->plateid = $plateid;
		$res = $selftoplate->add();
		if($res){
			return true;	//ADD SUCCESS
		}else{
			return false;	//ADD ERROR
		}
	}

	/*
	 * 更换图片
	 */
	public function saveimgdata(){

		$id = I('post.id');
		$selfhelpresourceid = I('post.selfhelpresourceid');
		$plateid = I('post.plateid');
		$imgdata = I('post.imgdata');

		$selftoplate = D(C('TABLE_SELFHELPRESOURCEPLATE'));
		$data['resimg'] = $imgdata;
		$res = $selftoplate->where(array('id'=>$id,'selfhelpresourceid'=>$selfhelpresourceid,'plateid'=>$plateid))->save($data);
		if($res){
			echo 1;	//SUCCESS
		}else{
			echo 0;	//ERROR
		}

	}

	/*
	 * 更换版块删除当前所更换的图片
	 */
	public function deletenowimg(){
		$nowid = I('get.nowid');
		if(!empty($nowid)){
			$selftoplate = D(C('TABLE_SELFHELPRESOURCEPLATE'));
			$data['resimg'] = null;
			$res = $selftoplate->where(array('id'=>$nowid))->save($data);
			if($res){
				echo 1;
			}else{
				echo 0;
			}
		}
	}


}